from kivy.app import App
from kivy.properties import DictProperty, ObjectProperty
from kivy.uix.widget import Widget
from kivy.uix.screenmanager import SlideTransition

from goblinoid.feed.feedview import FeedView
from goblinoid.root.screenstack import ScreenStack, UniqueScreenStack
from goblinoid.root.actionview import ActionLayout
from goblinoid.profile.profilescreen import ProfileScreen
from goblinoid.profile.activityscreen import ActivityScreen
from goblinoid.profile.profileview import ProfileView
from goblinoid.media.mediaviews import UploadView
from goblinoid.feed.feedview import FeedScreen
from goblinoid.login.loginview import LoginScreen, WebFinger, Verify


class ScreenProvider(Widget):
    screen_class = ObjectProperty()

    def get(self, **kwargs):
        return self.screen_class(**kwargs)


class ScreenWithManagerProvider(ScreenProvider):
    screen_providers = DictProperty()
    screen_manager_provider = ObjectProperty()
    screen_stack = ObjectProperty()
    sm_transition = ObjectProperty()

    def on_screen_providers(self, instance, screen_providers):
        self.screen_manager_provider = ScreenManagerProvider(
            screen_providers=screen_providers)

    def get(self, **kwargs):
        return self.screen_class(
            screen_stack=self.screen_stack,
            screen_provider=self.screen_manager_provider,
            sm_transition=self.sm_transition,
            **kwargs)


class ScreenManagerProvider(ScreenProvider):
    screen_providers = DictProperty()

    def has_screen(self, name):
        return name in self.screen_providers

    def get_screen(self, **kwargs):
        class_name = kwargs['class_name']
        if class_name in self.screen_providers:
            provider, priority = self.screen_providers[class_name]
            return provider().get(transition_priority=priority, **kwargs)


class SettingsProvider(ScreenProvider):
    pass


class ActivityProvider(ScreenProvider):
    def __init__(self, **kwargs):
        super(ActivityProvider, self).__init__(**kwargs)
        self.screen_class = ActivityScreen

    def get(self, **kwargs):
        activity_service = App.get_running_app().pump_service.activity_manager.get_activity_service(
            kwargs['activity_id'])
        return self.screen_class(activity_service=activity_service, **kwargs)


class ProfileProvider(ScreenProvider):

    def __init__(self, **kwargs):
        super(ProfileProvider, self).__init__(**kwargs)
        self.screen_class = ProfileScreen

    def get(self, **kwargs):
        profile_service = App.get_running_app().pump_service.profile_manager.get_profile_service(
            kwargs['profile_id'])
        return self.screen_class(profile_service, **kwargs)


class FeedScreenProvider(ScreenProvider):

    def __init__(self, **kwargs):
        super(FeedScreenProvider, self).__init__(**kwargs)
        self.screen_class = FeedScreen

    def get(self, **kwargs):
        feed_service = App.get_running_app().pump_service.feed_service
        if not kwargs.has_key('dynamic_props'):
            kwargs['dynamic_props'] = dict([('rv_height', 0)])
        print (kwargs)
        return self.screen_class(
            feed_service=feed_service,
            **kwargs)


class UserProfileProvider(ScreenWithManagerProvider):
    def __init__(self, **kwargs):
        super(UserProfileProvider, self).__init__(**kwargs)
        self.screen_providers = dict([
            ('profile', (ProfileProvider, 0)),
            ('activity', (ActivityProvider, 0)),
            ])
        self.screen_class = ProfileView
        self.screen_stack = ScreenStack()


class FeedProvider(ScreenWithManagerProvider):
    def __init__(self, **kwargs):
        super(FeedProvider, self).__init__(**kwargs)
        self.screen_providers = dict([
            ('feed_rv', (FeedScreenProvider, 0)),
            ('profile', (ProfileProvider, 0)),
            ('activity', (ActivityProvider, 0)),
            ])
        self.screen_class = FeedView
        self.screen_stack = ScreenStack()


class UploadProvider(ScreenWithManagerProvider):
    def __init__(self, **kwargs):
        super(UploadProvider, self).__init__(**kwargs)
        self.screen_class = UploadView
        self.screen_stack = ScreenStack()


class ActionProvider(ScreenWithManagerProvider):

    def __init__(self, **kwargs):
        super(ActionProvider, self).__init__(**kwargs)
        self.screen_providers = dict([
            ('settings', (SettingsProvider, 0)),
            ('feed', (FeedProvider, 1)),
            ('upload', (UploadProvider, 2)),
            ('profile', (UserProfileProvider, 3)),
            ])
        self.screen_class = ActionLayout
        self.screen_stack = UniqueScreenStack()
        self.sm_transition = SlideTransition(duration=0.2)


class WebFingerProvider(ScreenProvider):

    def __init__(self, **kwargs):
        super(WebFingerProvider, self).__init__(**kwargs)
        self.screen_class = WebFinger


class VerifyProvider(ScreenProvider):

    def __init__(self, **kwargs):
        super(VerifyProvider, self).__init__(**kwargs)
        self.screen_class = Verify


class LoginProvider(ScreenWithManagerProvider):

    def __init__(self, **kwargs):
        super(LoginProvider, self).__init__(**kwargs)
        self.screen_providers = dict([
            ('webfinger', (WebFingerProvider, 1)),
            ('verify', (VerifyProvider, 2))])
        self.screen_class = LoginScreen
        self.screen_stack = ScreenStack()
        self.sm_transition = SlideTransition(duration=0.2)
